{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "-----\n",
    "* 文档内容：图像识别（认知服务）\n",
    "* 记录时间：22春_API_AI_ML_week05\n",
    "* 电子笔记撰写者：魏宏奋\n",
    "-----"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 上周学习内容\n",
    "### 实践：宿舍人脸识别门禁系统\n",
    "\n",
    "> 1. 创建FaceSet\n",
    "> 1. 查询FaceSet\n",
    "> 1. 存入face_token数据\n",
    "> 1. 获取FaceSet信息数据\n",
    "> 1. 人脸搜索\n",
    "> 1. 人脸对比"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 本周学习内容\n",
    "### 百度AI开放平台 - 图像识别（认知服务）"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 通用物体和场景识别\n",
    "\n",
    "* 接口描述\n",
    "> 该请求用于通用物体及场景识别，即对于输入的一张图片（可正常解码，且长宽比适宜），输出图片中的多个物体及场景标签。\n",
    "\n",
    "* 请求示例\n",
    "\n",
    "> HTTP 方法：POST\n",
    "\n",
    "* 请求URL \n",
    "> https://aip.baidubce.com/rest/2.0/image-classify/v2/advanced_general"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 鉴权认证机制（获取access_token）\n",
    "* 获取Access Token"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'refresh_token': '25.7a5f52278d14fd12ee369baf2fb39169.315360000.1970316851.282335-25904827', 'expires_in': 2592000, 'session_key': '9mzdC3nG6ZPgj+/LxFML5LllDLiQI4hZ4lqKdrIeZtFv193UCfH0f2sHYtEfyuA49S83N5fH5IefUsRpsihBH+/LScFyxQ==', 'access_token': '24.0a03aebb6f68cfc474a93ccd3eceb6eb.2592000.1657548851.282335-25904827', 'scope': 'public vis-classify_dishes vis-classify_car brain_all_scope vis-classify_animal vis-classify_plant brain_object_detect brain_realtime_logo brain_dish_detect brain_car_detect brain_animal_classify brain_plant_classify brain_ingredient brain_advanced_general_classify brain_custom_dish brain_poi_recognize brain_vehicle_detect brain_redwine brain_currency brain_vehicle_damage brain_multi_ object_detect wise_adapt lebo_resource_base lightservice_public hetu_basic lightcms_map_poi kaidian_kaidian ApsMisTest_Test权限 vis-classify_flower lpq_开放 cop_helloScope ApsMis_fangdi_permission smartapp_snsapi_base smartapp_mapp_dev_manage iop_autocar oauth_tp_app smartapp_smart_game_openapi oauth_sessionkey smartapp_swanid_verify smartapp_opensource_openapi smartapp_opensource_recapi fake_face_detect_开放Scope vis-ocr_虚拟人物助理 idl-video_虚拟人物助理 smartapp_component smartapp_search_plugin avatar_video_test b2b_tp_openapi b2b_tp_openapi_online smartapp_gov_aladin_to_xcx', 'session_secret': 'fe55e8f31d8affba4e32799a65b0c129'}\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "import requests \n",
    "\n",
    "# client_id 为官网获取的AK， client_secret 为官网获取的SK\n",
    "host = 'https://aip.baidubce.com/oauth/2.0/token?'\n",
    "# 准备URL参数\n",
    "payload = {\n",
    "    'grant_type':'client_credentials',\n",
    "    'client_id':'Cqa8tNYu2Aqb4xUj6njjau0a',\n",
    "    'client_secret':'5fhPd2gSWbPbq26vyDX1DQCax35GNkxL'\n",
    "}\n",
    "\n",
    "response = requests.get(host,params=payload)\n",
    "if response:\n",
    "    print(response.json())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'refresh_token': '25.7a5f52278d14fd12ee369baf2fb39169.315360000.1970316851.282335-25904827',\n",
       " 'expires_in': 2592000,\n",
       " 'session_key': '9mzdC3nG6ZPgj+/LxFML5LllDLiQI4hZ4lqKdrIeZtFv193UCfH0f2sHYtEfyuA49S83N5fH5IefUsRpsihBH+/LScFyxQ==',\n",
       " 'access_token': '24.0a03aebb6f68cfc474a93ccd3eceb6eb.2592000.1657548851.282335-25904827',\n",
       " 'scope': 'public vis-classify_dishes vis-classify_car brain_all_scope vis-classify_animal vis-classify_plant brain_object_detect brain_realtime_logo brain_dish_detect brain_car_detect brain_animal_classify brain_plant_classify brain_ingredient brain_advanced_general_classify brain_custom_dish brain_poi_recognize brain_vehicle_detect brain_redwine brain_currency brain_vehicle_damage brain_multi_ object_detect wise_adapt lebo_resource_base lightservice_public hetu_basic lightcms_map_poi kaidian_kaidian ApsMisTest_Test权限 vis-classify_flower lpq_开放 cop_helloScope ApsMis_fangdi_permission smartapp_snsapi_base smartapp_mapp_dev_manage iop_autocar oauth_tp_app smartapp_smart_game_openapi oauth_sessionkey smartapp_swanid_verify smartapp_opensource_openapi smartapp_opensource_recapi fake_face_detect_开放Scope vis-ocr_虚拟人物助理 idl-video_虚拟人物助理 smartapp_component smartapp_search_plugin avatar_video_test b2b_tp_openapi b2b_tp_openapi_online smartapp_gov_aladin_to_xcx',\n",
       " 'session_secret': 'fe55e8f31d8affba4e32799a65b0c129'}"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "response.json()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'24.0a03aebb6f68cfc474a93ccd3eceb6eb.2592000.1657548851.282335-25904827'"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "access_token = response.json()['access_token']\n",
    "access_token"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 通用物体和场景识别\n",
    "* 请求参数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>参数</th>\n",
       "      <th>是否必选</th>\n",
       "      <th>类型</th>\n",
       "      <th>可选值范围</th>\n",
       "      <th>说明</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>image</td>\n",
       "      <td>和url二选一</td>\n",
       "      <td>string</td>\n",
       "      <td>-</td>\n",
       "      <td>图像数据，base64编码，要求base64编码后大小不超过4M，最短边至少15px，最长边...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>url</td>\n",
       "      <td>和image二选一</td>\n",
       "      <td>string</td>\n",
       "      <td>-</td>\n",
       "      <td>图片完整URL，URL长度不超过1024字节，URL对应的图片base64编码后大小不超过4...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>baike_num</td>\n",
       "      <td>否</td>\n",
       "      <td>integer</td>\n",
       "      <td>-</td>\n",
       "      <td>用于控制返回结果是否带有百科信息，若不输入此参数，则默认不返回百科结果；若输入此参数，会根据...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          参数       是否必选       类型 可选值范围  \\\n",
       "0      image    和url二选一   string     -   \n",
       "1        url  和image二选一   string     -   \n",
       "2  baike_num          否  integer     -   \n",
       "\n",
       "                                                  说明  \n",
       "0  图像数据，base64编码，要求base64编码后大小不超过4M，最短边至少15px，最长边...  \n",
       "1  图片完整URL，URL长度不超过1024字节，URL对应的图片base64编码后大小不超过4...  \n",
       "2  用于控制返回结果是否带有百科信息，若不输入此参数，则默认不返回百科结果；若输入此参数，会根据...  "
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pd.read_html('https://ai.baidu.com/ai-doc/IMAGERECOGNITION/Xk3bcxe21')[2]\n",
    "# pd.read_html 可以将html的页面信息转化成表格（更具可读性）"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 示例代码"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```\n",
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "通用物体和场景识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/image-classify/v2/advanced_general\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('[本地文件]', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "params = {\"image\":img}\n",
    "access_token = '[调用鉴权接口获取的token]'\n",
    "request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=params, headers=headers)\n",
    "if response:\n",
    "    print (response.json())\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64   #该函数是把图片转化成对应的格式\n",
    "\n",
    "'''\n",
    "通用物体和场景识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/image-classify/v2/advanced_general\"\n",
    "# 二进制方式打开图片文件\n",
    "# （1）图片文件准备\n",
    "# f = open('C:/Users/desktop.ini', 'rb')   # 绝对路径\n",
    "f = open('笔记本电脑.jpg','rb')  # 相对路径    注：rb（read bytes 读取二进制文件格式）\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "# params = {\"image\":img}\n",
    "# （2）酬载准备\n",
    "# URL参数\n",
    "payload = {\n",
    "    'access_token':access_token,\n",
    "    'image':img,\n",
    "    'baike_num':5\n",
    "}\n",
    "\n",
    "# request_url = request_url + \"?access_token=\" + access_token\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, params=payload, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 菜品识别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'result_num': 5, 'result': [{'name': '红烧肉', 'calorie': '227', 'probability': '0.860848', 'baike_info': {'baike_url': 'http://baike.baidu.com/item/%E7%BA%A2%E7%83%A7%E8%82%89/571767', 'image_url': 'https://bkimg.cdn.bcebos.com/pic/f31fbe096b63f624c51a87b68944ebf81b4ca3c4', 'description': '红烧肉(拼音： hóng shāo ròu、注音：ㄏㄨㄥˊ ㄕㄠ ㄖㄡˋ)，一道著名的大众菜肴，各大菜系都有自己特色的红烧肉。其以五花肉为制作主料，最好选用肥瘦相间的三层肉(五花肉)来做，锅具以砂锅为主，做出来的肉肥瘦相间，香甜松软，营养丰富，入口即化。红烧肉在我国各地流传甚广，做法多达二三十种，具有一定的营养价值。'}, 'has_calorie': True}, {'name': '茄子煲', 'calorie': '137', 'probability': '0.0253862', 'baike_info': {'baike_url': 'http://baike.baidu.com/item/%E8%8C%84%E5%AD%90%E7%85%B2/2376198', 'image_url': 'https://bkimg.cdn.bcebos.com/pic/7dd98d1001e9390112e6f92173ec54e737d196cc', 'description': '茄子煲是用茄子制作的一道家常菜，属于粤菜系。茄子的营养比较丰富，含有蛋白质、脂肪、碳水化合物、维生素以及钙、磷、铁等多种营养成分。主要有：水煮酿茄子煲、肉沫茄子煲、砂锅萝卜茄子煲等。'}, 'has_calorie': True}, {'name': '三杯鸡', 'calorie': '175', 'probability': '0.0165482', 'baike_info': {}, 'has_calorie': True}, {'name': '卤肉饭', 'calorie': '204', 'probability': '0.0143258', 'baike_info': {'baike_url': 'http://baike.baidu.com/item/%E5%8D%A4%E8%82%89%E9%A5%AD/3135811', 'image_url': 'https://bkimg.cdn.bcebos.com/pic/d6ca7bcb0a46f21fbe093e97206c7c600c3386440094', 'description': '卤肉饭是江南一带的传统名小吃，制作原料主要有猪五花肉、鹌鹑蛋、米饭等，肥而不腻、甜咸适口、香浓四溢。带有厚重口感的卤肉，配上一碗蒸的不软不硬的白饭，每一粒米都吸透黑红的汤汁。'}, 'has_calorie': True}, {'name': '梅干菜扣肉', 'calorie': '463', 'probability': '0.0103191', 'baike_info': {'baike_url': 'http://baike.baidu.com/item/%E6%A2%85%E5%B9%B2%E8%8F%9C%E6%89%A3%E8%82%89/1947121', 'image_url': 'https://bkimg.cdn.bcebos.com/pic/6a63f6246b600c33874425927807460fd9f9d72a0b1f', 'description': '梅干菜扣肉是传统特色菜肴，用连皮五花肉烧好后，再在碗里放上腌制的梅干菜，这样梅干菜扣肉就好了。'}, 'has_calorie': True}], 'log_id': 1535626538296097114}\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "菜品识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/image-classify/v2/dish\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('hongshaorou.jpg', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "payload={\n",
    "    'access_token':access_token,\n",
    "    'image':img,\n",
    "    'baike_num':5,\n",
    "    \"top_num\":5\n",
    "}\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=payload, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 植物识别"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'result': [{'score': 0.7713093, 'name': '千佛手', 'baike_info': {}}, {'score': 0.2673562, 'name': '东云', 'baike_info': {'baike_url': 'http://baike.baidu.com/item/%E4%B8%9C%E4%BA%91/1134931', 'image_url': 'https://bkimg.cdn.bcebos.com/pic/f3d3572c11dfa9ec0230f46868d0f703918fc1b7', 'description': '东云(Echeveria agavoides Lemaire)是景天科拟石莲花属的多肉植物，原产于墨西哥多州。特征是形状锋利的叶片。拉丁文种加词意为“似龙舌兰的”，指其叶片形状与排列方式与龙舌兰属植物相似。约于1861年前后被引入人工栽培中，于1863年发表。果实椭圆形，中等大小，果皮乳白色，阳面有红色，果肉白色，肉质柔软，多汁，味酸甜，粘核；含可溶性固形物10%，品质中等。'}}, {'score': 0.103502505, 'name': '瓦松', 'baike_info': {'baike_url': 'http://baike.baidu.com/item/%E7%93%A6%E6%9D%BE/894549', 'image_url': 'https://bkimg.cdn.bcebos.com/pic/a5c27d1ed21b0ef4a41ebfcfdec451da80cb3ea8', 'description': '瓦松，中药名。为景天科植物瓦松Orosta.chys fimbriata(Turcz.)Berg.的干燥地上部分。夏、秋二季花开时采收，除去根及杂质，晒干。'}}, {'score': 0.09724221, 'name': '千佛手景天', 'baike_info': {'baike_url': 'http://baike.baidu.com/item/%E6%99%AF%E5%A4%A9%E5%B1%9E/2896974', 'image_url': 'https://bkimg.cdn.bcebos.com/pic/9f2f070828381f304677d34aa9014c086f06f0e3', 'description': '一年生或多年生草本或亚灌木，无毛或有毛。多数叶对生，罕见三叶轮生，有时小而覆瓦状排列。花排成顶生的聚伞花序，常偏生于分枝之一侧；萼4~5裂；花瓣4~5，分离或基部合生；雄蕊与花瓣同数或2倍之；心皮4~5，离生，有时基部连合，有胚珠多颗。果为一蓇葖。'}}, {'score': 0.03839492, 'name': '翡翠景天', 'baike_info': {}}], 'log_id': 1535626577704222228}\n"
     ]
    }
   ],
   "source": [
    "# encoding:utf-8\n",
    "\n",
    "import requests\n",
    "import base64\n",
    "\n",
    "'''\n",
    "植物识别\n",
    "'''\n",
    "\n",
    "request_url = \"https://aip.baidubce.com/rest/2.0/image-classify/v1/plant\"\n",
    "# 二进制方式打开图片文件\n",
    "f = open('多肉植物.jpg', 'rb')\n",
    "img = base64.b64encode(f.read())\n",
    "\n",
    "payload={\n",
    "    'access_token':access_token,\n",
    "    'image':img,\n",
    "    'baike_num':5,\n",
    "    \"top_num\":5\n",
    "}\n",
    "\n",
    "\n",
    "headers = {'content-type': 'application/x-www-form-urlencoded'}\n",
    "response = requests.post(request_url, data=payload, headers=headers)\n",
    "if response:\n",
    "    print (response.json())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* 小程序成品应用参考  \n",
    "> 微信小程序搜索：识相Insight"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
